package com.lx.lxsql.entity;

//说明: Statement

import com.lx.util.LX;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**{ ylx } 2021/9/19 21:20 */
public class StatementInformation {
    /**
     * 文字类型
     */
    private SIType siType;
    private String txt;

    public StatementInformation(SIType siType, String txt) {
        this.siType = siType;
        this.txt = txt;
    }

    public SIType getSiType() {
        return siType;
    }

    public void setSiType(SIType siType) {
        this.siType = siType;
    }

    public String getTxt() {
        return txt;
    }

    public void setTxt(String txt) {
        this.txt = txt;
    }





    public static final String SYMBOL_REGULAR="\\+|-|\\*|/|\\(|\\)|,|=|\\!=|>=|<=|>|<";
    //说明: 解析字符串 '
    /**{ ylx } 2021/9/19 19:38 */
    public static void parseSqlnList(String data , List<StatementInformation> sis) {
        StringBuilder sb = new StringBuilder(data);
        int len1,len2 = -1;
        while ((len1 = sb.indexOf("'")) != -1){
            //截取到 开始'
            //检查'前面的字符串是否异常
            String start = sb.substring(0,len1);
            toStatementInformationList(start,SYMBOL_REGULAR,sis);
            //获取后续的'非''
            int s1 = len1+1;
            do{
                len2 = sb.indexOf("'", s1);
                if (sb.length()==len2 || '\'' != sb.charAt(len2+1)){
                    //当前' 后面有字符串且不是'
                    break;
                }
                s1 = len2+2;
            }while(true);
            if (len2 == -1){
                LX.exMsg("解析异常:"+data);
            }
            String str = sb.substring(len1+1,len2).replace("''","'");
            sis.add(new StatementInformation(SIType.STRING, str));
            sb.delete(0,len2+1);
        }
        if (sb.length()>0){
            toStatementInformationList(sb.toString(),SYMBOL_REGULAR,sis);
        }
    }
    //说明: 解析字符串为字段
    /**{ ylx } 2021/9/19 20:59 */
    public static void toStatementInformationList(String str, String symbolRegular , List<StatementInformation> sis) {
        Arrays.asList(str.split("\\s+")).forEach(s->{
            //按照空白字符拆分
            Pattern p = Pattern.compile(symbolRegular);
            Matcher m = p.matcher(s);
            int start,end = 0;
            do {
                if (m.find()) {
                    start = m.start();
                    if (start>end){
                        String s1= s.substring(end,start);
                        sis.add(new StatementInformation(SIType.getWordType(s1),s1));
                    }
                    String s1 = m.group();
                    sis.add(new StatementInformation(SIType.getSymbolType(s1),s1));
                    end = m.end();
                } else {
                    if (s.length()>end){
                        String s1= s.substring(end);
                        sis.add(new StatementInformation(SIType.getWordType(s1),s1));
                    }
                    break;
                }
            }while (true);
        });
    }

    @Override
    public String toString() {
        return "{" +
                "siType=" + siType +
                ", txt='" + txt + '\'' +
                '}';
    }
}
